import math

import select_sort

# 一组数据
nums = [7, 3, 1, 34, 6, 67, 23, 12, 9, 11]
#  0  1  2  3  4  5   6   7   8   9
# [1, 3, 6, 7, 9, 11, 12, 23, 34, 67]

def bin_search(nums, search_num):
  # 先对这组数据进行排序
  sorted_nums = select_sort.select_sort(nums)
  # low从头开始
  low_index = 0
  # high从尾开始
  high_index = len(sorted_nums) - 1
  # 当寻找的数比中间元素小时
  while low_index < high_index:
    mid_index = math.floor((low_index + high_index) / 2)
    middle = sorted_nums[mid_index]
    if search_num == middle:
      return mid_index
    elif search_num < sorted_nums[mid_index]:
      high_index = mid_index - 1  # 高位索引改为上轮中间元素的前一个
    elif search_num > sorted_nums[mid_index]:
      low_index = mid_index + 1
  return "没有该元素"

if __name__ == '__main__':
  print(f"所要查找的元素在第{bin_search(nums, 3)}位！")
